<?php

class User extends Cartalyst\Sentry\Users\Eloquent\User implements Robbo\Presenter\PresentableInterface {

    public function __construct (array $attributes = array()) {}
    public function getPresenter () {}
    
    
    /**
     * get all user where delete = false
     * @author Thuan Truong
     */
    public function getAllUser () {
        $users = User::where('is_deleted', '=', 0)->get();
        return $users;
    }
    
    /**
     * find user by id
     * @author Thuan Truong
     */
    public function findUserByID ($id) {
        $user = DB::table('users')->where('id', $id)->first();
        
        if (empty($user)) {
            App::abort(404);
        }
        return $user; 
    }
    
    /**
     * delete user
     * @author Thuan Truong
     */
    public function deleteUser($id) {
        DB::table('users')
            ->where('id', $id)
            ->update(array('is_deleted' => 1));
    }
    
    
    /**
     * check login with admin
     * @author Thuan Truong
     */
    public function adminLogin ($input) {
        
    }
    
    /**
      * validate for login
      * @author Thuan Truong
      * @param input
      * @return array
      */
     public function validateLogin($input) {
        $rules = array(
            'email' => 'required|min:4|max:100|email',
            'password' => 'required|min:5|max:50',
        );
            
        $validator = Validator::make($input, $rules);
        return $validator;
     }
     
     
     /**
     * Check login user
     * @author Thuan Truong
     * @param email, password or activation_code
     * @return integer ( 0->false, 1->user login with email)
     */
     public function userLogin($username, $password)
     {        
         $userEmail = $this->findUserbyEmail($username);
         
         if (!empty($userEmail)) {  //login with email 
            $user = User::find($userEmail[0]->id);
            if (Hash::check($password, $user->password)) return 1;
            return 0;
         } else{ 
             return 0;
         }
     }
        
        
     /**
      * find User by email
      * @author Thuan Truong
      * @param email
      * @return array
      */
     public function findUserbyEmail($email) {
         $users = DB::table('users')->where('is_deleted', '=', 0)
             ->where('email', '=', $email)
             ->get();
         return $users;
     }
     
     
     /**
      * Get all admins
      * @author Duc Nguyen
      */
     public function getAdmins() {
        $adminGroup = Sentry::findGroupByName('admin');
        $admins = Sentry::findAllUsersInGroup($adminGroup);
        return $admins;
     }
     
     /**
      * Get all managers
      * @author Duc Nguyen
      */
     public function getManagers() {
        $managerGroup = Sentry::findGroupByName('manager');
        $managers = Sentry::findAllUsersInGroup($managerGroup);
        return $managers;
     }

     
     /**
      * Get all users
      * @author Duc Nguyen
      */
     public function getUsers() {
        $userGroup = Sentry::findGroupByName('user');
        $users = Sentry::findAllUsersInGroup($userGroup);
        return $users;
     }
	 
	 
	 
	 /**
      * validate for register
      * @author Thuan Truong
      */
     public function validateRegister($input) {
        $rules = array(
            'full_name' => 'required|min:3|max:250',
            'email' => 'required|email|unique:users,email,NULL,id,is_deleted,0|max:100',
            'address' => 'required|min:4|max:500',
            'province' => 'required',
            'password' => 'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6'
        );
            
        $validator = Validator::make($input, $rules);
        return $validator;
     }
}